package rullekake.beans;

import java.util.LinkedList;
import java.util.ResourceBundle;
import rullekake.entities.Outcome;
import rullekake.beans.util.JsfUtil;
import rullekake.facade.OutcomeFacade;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.SelectItem;
import rullekake.beans.util.LocaleHelper;
import rullekake.entities.Course;

/**
 * Klasse som håndterer oppretting/visning/endring av læringsutbytter
 *
 */

@ManagedBean (name="outcomeController")
@SessionScoped
public class OutcomeController {

    private Outcome current;
    @EJB private rullekake.facade.OutcomeFacade ejbFacade;

    public OutcomeController() {
    }

     /**
     * Metode som returnerer de forskjellige typer læringsutbytter i tilpasset språk
     */

    public SelectItem[] getTypes() {
        SelectItem[] types = new SelectItem[3];
        types[0] = new SelectItem(1, ResourceBundle.getBundle(LocaleHelper.getBundleName()).getString("outcomeType1"));
        types[1] = new SelectItem(2, ResourceBundle.getBundle(LocaleHelper.getBundleName()).getString("outcomeType2"));
        types[2] = new SelectItem(3, ResourceBundle.getBundle(LocaleHelper.getBundleName()).getString("outcomeType3"));
        return types;
    }

    public Outcome getSelected() {
        if (current == null) {
            current = new Outcome();
        }
        return current;
    }

    public void setCurrent(Outcome outcome) {
        current = outcome;
    }

    private OutcomeFacade getFacade() {
        return ejbFacade;
    }

    public String prepareCreate() {
        current = new Outcome();
        return null;
    }

    public String create() {
        try {
            getFacade().create(current);
            Course course = current.getCourse();
            if(course.getOutcomeCollection() == null) {
                course.setOutcomeCollection(new LinkedList<Outcome>());
            }
            course.getOutcomeCollection().add(current);
            JsfUtil.addSuccessMessage("OutcomeCreated");
            return "course/Edit";
        } catch (Exception e) {
            JsfUtil.addErrorMessage("PersistenceErrorOccured");
            return null;
        }
    }

    public String update() {
        try {
            getFacade().edit(current);
            JsfUtil.addSuccessMessage("OutcomeUpdated");
            return "course/Edit";
        } catch (Exception e) {
            JsfUtil.addErrorMessage("PersistenceErrorOccured");
            return null;
        }
    }

    public String destroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage("OutcomeDeleted");
        } catch (Exception e) {
            JsfUtil.addErrorMessage("PersistenceErrorOccured");
        }
        return "course/Edit";
    }

    @FacesConverter(forClass=Outcome.class)
    public static class OutcomeControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            OutcomeController controller = (OutcomeController)facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "outcomeController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuilder sb = new StringBuilder();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Outcome) {
                Outcome o = (Outcome) object;
                return getStringKey(o.getOutcomeId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: "+OutcomeController.class.getName());
            }
        }

    }

}
